Option Strict On
Option Explicit On

Imports System

Public Class Utility
   Public Shared Function GetTableName( _
            ByVal origName As String) _
            As String
      Dim tablename As String = origName
      Return tablename
   End Function

   Public Shared Function GetViewName( _
            ByVal origName As String, _
            ByVal viewIdType As CodeGenerationSupport.ViewIDType, _
            ByVal viewText As String) _
            As String
      Dim tablename As String = origName
      If viewIdType = CodeGenerationSupport.ViewIDType.Prefix Then
         If origName.StartsWith(viewText) Then
            tablename = origName.Substring(viewText.Length)
         End If
      ElseIf viewIdType = CodeGenerationSupport.ViewIDType.Suffix Then
         If origName.EndsWith(viewText) Then
            tablename = origName.Substring(0, origName.Length - viewText.Length)
         End If
      End If
      Return tablename
   End Function

   Public Shared Function GetDisplayName( _
             ByVal rows() As Data.DataRow, _
             ByVal tableName As String) _
             As String
      Dim firstString As String = Nothing
      Dim columnName As String
      Dim simpleName As String = Nothing
      Dim tableTableName As String = Nothing
      Dim anyName As String = Nothing
      Dim anyDesc As String = Nothing
      Dim testTableName As String = tableName.ToLower
      ' First see if we lack strings
      For Each row As Data.DataRow In rows
         Select Case CStr(row("DATA_TYPE"))
            Case "char", "varchar", "nchar", "nvarchar"
               ' Changed to original 10/20/04 KAD
               columnName = row("COLUMN_NAME").ToString
               Dim testColumnName As String = columnName.ToLower
               'columnName = Utility.Tools.FixName(row("COLUMN_NAME").ToString, _
               '            Me.mremoveAllPrefixes)
               If firstString Is Nothing Then
                  firstString = columnName
               End If
               If testColumnName = KadGen.Utility.Tools.GetSingular(testTableName) & "name" Then
                  tableTableName = columnName
               ElseIf testColumnName = KadGen.Utility.Tools.GetPlural(testTableName) & "name" Then
                  tableTableName = columnName
               ElseIf testColumnName = "name" Then
                  simpleName = columnName
               ElseIf anyName Is Nothing AndAlso testColumnName.IndexOf("name") >= 0 Then
                  anyName = columnName
               ElseIf anyDesc Is Nothing AndAlso testColumnName.IndexOf("desc") >= 0 Then
                  anyDesc = columnName
               End If
         End Select
      Next

      If Not tableTableName Is Nothing Then
         Return tableTableName
      ElseIf Not simpleName Is Nothing Then
         Return simpleName
      ElseIf Not anyName Is Nothing Then
         Return anyName
      ElseIf Not anyDesc Is Nothing Then
         Return anyDesc
      ElseIf Not firstString Is Nothing Then
         Return firstString
      Else
         ' Changed to original 10/20/04 KAD
         Return rows(0)("COLUMN_NAME").ToString
         'Return Utility.Tools.FixName(rows(0)("COLUMN_NAME").ToString, _
         '            Me.mremoveAllPrefixes)
      End If
   End Function

   Public Shared Function MakeCaption( _
         ByVal s As String) _
         As String
      Return KadGen.Utility.Tools.SpaceAtCaps(s)
   End Function

   Public Shared Function FixName(ByVal tablename As String, _
            ByVal removeAllPrefixes As Boolean, _
            ByVal prefixes As String, _
            ByVal friendlyCaps As Boolean, _
            ByVal removeUnderscores As Boolean, _
            ByVal keywordTreatment As CodeGenerationSupport.KeywordTreatment, _
            ByVal targetLanguage As CodeGenerationSupport.TargetLanguage, _
            ByVal blankTreatment As CodeGenerationSupport.BlankTreatment) _
            As String
      Return KadGen.Utility.Tools.FixName(tablename, removeAllPrefixes, prefixes, _
               friendlyCaps, removeUnderscores, keywordTreatment.ToString(), _
               targetLanguage.ToString, blankTreatment.ToString)
   End Function

   Public Shared Function GetPrefix(ByVal originalName As String, _
            ByVal prefixes As String, _
            ByVal removeAllPrefixes As Boolean) _
            As String
      Return KadGen.Utility.Tools.GetPrefix(originalName, prefixes, removeAllPrefixes)
   End Function

   Public Shared Function GetSingular(ByVal name As String) As String
      Return KadGen.Utility.Tools.GetSingular(name)
   End Function

   Public Shared Function GetPlural(ByVal name As String) As String
      Return KadGen.Utility.Tools.GetPlural(name)
   End Function

   Public Shared Function GetXmlFriendlyName(ByVal originalName As String) As String
      Return KadGen.Utility.Tools.FixName(originalName)
   End Function

   Public Shared Function GetNetTypeFromSqlType(ByVal sqlType As String) As String
      Return KadGen.Utility.Tools.GetNETTypeFromSQLType(sqlType)
   End Function

   'Public Shared Function IsPrimaryKey( _
   '                ByVal rowTable As Data.DataRow, _
   '                ByVal rowColumn As Data.DataRow) _
   '                As Boolean
   '   Dim rowPrimaryKey As Data.DataRow = GetPrimaryKeyConstraint(rowTable)
   '   Dim rowKeyColumns() As Data.DataRow
   '   If Not rowPrimaryKey Is Nothing Then
   '      rowKeyColumns = mDataSet.Tables("KeyColumns").Select( _
   '                                 ConstraintMatch(rowPrimaryKey) & _
   '                                          " AND COLUMN_NAME='" & _
   '                                          rowColumn("COLUMN_NAME").ToString & "'")
   '      Return (rowKeyColumns.GetLength(0) > 0)
   '   End If
   'End Function

   Public Shared Function GetInteger(ByVal value As Object) As Int32
      If value Is DBNull.Value Then
         Return 0
      End If
      Return CInt(value)
   End Function

   Public Shared Function IsRetrieveRecordset( _
            ByVal spName As String, _
            ByVal selectPatterns() As String) _
            As Boolean
      For Each s As String In SelectPatterns
         If Text.RegularExpressions.Regex.IsMatch(spName, s) Then
            Return True
         End If
      Next
   End Function

   Public Shared Function IsSetSelectRecordset( _
            ByVal spName As String, _
            ByVal setSelectPatterns() As String) _
            As Boolean
      For Each s As String In SetSelectPatterns
         If Text.RegularExpressions.Regex.IsMatch(spName, s) Then
            Return True
         End If
      Next
   End Function


   Private Shared mTraceSource As Diagnostics.TraceSource
   Private Shared ReadOnly Property TraceSource() As Diagnostics.TraceSource
      Get
         If mTraceSource Is Nothing Then
            mTraceSource = New Diagnostics.TraceSource("CreateMetadata2007")
         End If
         Return mTraceSource
      End Get
   End Property
   Public Shared Sub LogForUser( _
            ByVal message As String, _
            ByVal id As LogId, _
            ByVal ParamArray wrapperBases() As MetadataWrapper.WrapperBase)
      'Console.WriteLine(message)
      'For Each base As MetadataWrapper.WrapperBase In wrapperBases
      '   Console.Write(Microsoft.VisualBasic.ControlChars.Tab)
      '   Console.WriteLine(String.Format("{0}: {1}", base.GetType.Name, base.IDString))
      'Next

      Dim wrapperMessage As New MetadataWrapper.WrapperMessage(message, Diagnostics.TraceEventType.Warning, id, wrapperBases)
      Console.WriteLine(wrapperMessage.ToString)
      wrapperMessage.Log(TraceSource)

   End Sub

End Class
